#!/bin/Rscript

#On-demand
on_100_1 <- read.table("risco_1_5_10/erro_0/on/profits.dat")$V1
on_100_5 <- read.table("risco_5_10_50/erro_0/on/profits.dat")$V1

#Erro 0%
op_100_1_0 <- read.table("risco_1_5_10/erro_0/rf/profits.dat")$V1
ut_100_1_0 <- read.table("risco_1_5_10/erro_0/ut/profits.dat")$V1
iosup_100_1_0 <- read.table("risco_1_5_10/erro_0/iosup/profits.dat")$V1
super_100_1_0 <- read.table("risco_1_5_10/erro_0/super/profits.dat")$V1

op_100_5_0 <- read.table("risco_5_10_50/erro_0/rf/profits.dat")$V1
ut_100_5_0 <- read.table("risco_5_10_50/erro_0/ut/profits.dat")$V1
iosup_100_5_0 <- read.table("risco_5_10_50/erro_0/iosup/profits.dat")$V1
super_100_5_0 <- read.table("risco_5_10_50/erro_0/super/profits.dat")$V1

gop_100_1_0 <- (op_100_1_0 - on_100_1) / abs(on_100_1)
gut_100_1_0 <- (ut_100_1_0 - on_100_1) / abs(on_100_1)
giosup_100_1_0 <- (iosup_100_1_0 - on_100_1) / abs(on_100_1)
gsuper_100_1_0 <- (super_100_1_0 - on_100_1) / abs(on_100_1)

gop_100_5_0 <- (op_100_5_0 - on_100_5) / abs(on_100_5)
gut_100_5_0 <- (ut_100_5_0 - on_100_5) / abs(on_100_5)
giosup_100_5_0 <- (iosup_100_5_0 - on_100_5) / abs(on_100_5)
gsuper_100_5_0 <- (super_100_5_0 - on_100_5) / abs(on_100_5)

#Erro 20%
op_100_1_20 <- read.table("risco_1_5_10/erro_20/rf/profits.dat")$V1
ut_100_1_20 <- read.table("risco_1_5_10/erro_20/ut/profits.dat")$V1
iosup_100_1_20 <- read.table("risco_1_5_10/erro_20/iosup/profits.dat")$V1
super_100_1_20 <- read.table("risco_1_5_10/erro_20/super/profits.dat")$V1

op_100_5_20 <- read.table("risco_5_10_50/erro_20/rf/profits.dat")$V1
ut_100_5_20 <- read.table("risco_5_10_50/erro_20/ut/profits.dat")$V1
iosup_100_5_20 <- read.table("risco_5_10_50/erro_20/iosup/profits.dat")$V1
super_100_5_20 <- read.table("risco_5_10_50/erro_20/super/profits.dat")$V1

gop_100_1_20 <- (op_100_1_20 - on_100_1) / abs(on_100_1)
gut_100_1_20 <- (ut_100_1_20 - on_100_1) / abs(on_100_1)
giosup_100_1_20 <- (iosup_100_1_20 - on_100_1) / abs(on_100_1)
gsuper_100_1_20 <- (super_100_1_20 - on_100_1) / abs(on_100_1)

gop_100_5_20 <- (op_100_5_20 - on_100_5) / abs(on_100_5)
gut_100_5_20 <- (ut_100_5_20 - on_100_5) / abs(on_100_5)
giosup_100_5_20 <- (iosup_100_5_20 - on_100_5) / abs(on_100_5)
gsuper_100_5_20 <- (super_100_5_20 - on_100_5) / abs(on_100_5)

#Erro -20%
op_100_1_m20 <- read.table("risco_1_5_10/erro_m20/rf/profits.dat")$V1
ut_100_1_m20 <- read.table("risco_1_5_10/erro_m20/ut/profits.dat")$V1
iosup_100_1_m20 <- read.table("risco_1_5_10/erro_m20/iosup/profits.dat")$V1
super_100_1_m20 <- read.table("risco_1_5_10/erro_m20/super/profits.dat")$V1

op_100_5_m20 <- read.table("risco_5_10_50/erro_m20/rf/profits.dat")$V1
ut_100_5_m20 <- read.table("risco_5_10_50/erro_m20/ut/profits.dat")$V1
iosup_100_5_m20 <- read.table("risco_5_10_50/erro_m20/iosup/profits.dat")$V1
super_100_5_m20 <- read.table("risco_5_10_50/erro_m20/super/profits.dat")$V1

gop_100_1_m20 <- (op_100_1_m20 - on_100_1) / abs(on_100_1)
gut_100_1_m20 <- (ut_100_1_m20 - on_100_1) / abs(on_100_1)
giosup_100_1_m20 <- (iosup_100_1_m20 - on_100_1) / abs(on_100_1)
gsuper_100_1_m20 <- (super_100_1_m20 - on_100_1) / abs(on_100_1)

gop_100_5_m20 <- (op_100_5_m20 - on_100_5) / abs(on_100_5)
gut_100_5_m20 <- (ut_100_5_m20 - on_100_5) / abs(on_100_5)
giosup_100_5_m20 <- (iosup_100_5_m20 - on_100_5) / abs(on_100_5)
gsuper_100_5_m20 <- (super_100_5_m20 - on_100_5) / abs(on_100_5)

#Calculando lucros gerais para 100 usuarios

total_ut <- c(ut_100_1_20, ut_100_1_0, ut_100_1_m20, ut_100_5_20, ut_100_5_0, ut_100_5_m20)
paste("Lucro geral UT 100: ", mean(total_ut), "[ ", 1.96*sd(total_ut)/sqrt(length(total_ut))+mean(total_ut), " : ", mean(total_ut) - 1.96*sd(total_ut)/sqrt(length(total_ut)), " ] ")

total_op <- c(op_100_1_20, op_100_1_0, op_100_1_m20, op_100_5_20, op_100_5_0, op_100_5_m20)
paste("Lucro geral RF 100: ", mean(total_op), "[ ", 1.96*sd(total_op)/sqrt(length(total_op))+mean(total_op), " : ", mean(total_op) - 1.96*sd(total_op)/sqrt(length(total_op)), " ] ")

total_iosup <- c(iosup_100_1_20, iosup_100_1_0, iosup_100_1_m20, iosup_100_5_20, iosup_100_5_0, iosup_100_5_m20)
paste("Lucro geral iosup 100: ", mean(total_iosup), "[ ", 1.96*sd(total_iosup)/sqrt(length(total_iosup))+mean(total_iosup), " : ", mean(total_iosup) - 1.96*sd(total_iosup)/sqrt(length(total_iosup)), " ] ")

total_super <- c(super_100_1_20, super_100_1_0, super_100_1_m20, super_100_5_20, super_100_5_0, super_100_5_m20)
paste("Lucro geral super 100: ", mean(total_super), "[ ", 1.96*sd(total_super)/sqrt(length(total_super))+mean(total_super), " : ", mean(total_super) - 1.96*sd(total_super)/sqrt(length(total_super)), " ] ")

t.test(total_ut, total_op, paired=TRUE, var.equal=FALSE)
t.test(total_ut, total_op, paired=TRUE, var.equal=FALSE, alternative="less")
t.test(total_ut, total_op, paired=TRUE, var.equal=FALSE, alternative="greater")

t.test(total_iosup, total_op, paired=TRUE, var.equal=FALSE)
t.test(total_iosup, total_op, paired=TRUE, var.equal=FALSE, alternative="less")
t.test(total_iosup, total_op, paired=TRUE, var.equal=FALSE, alternative="greater")

t.test(total_super, total_op, paired=TRUE, var.equal=FALSE)
t.test(total_super, total_op, paired=TRUE, var.equal=FALSE, alternative="less")
t.test(total_super, total_op, paired=TRUE, var.equal=FALSE, alternative="greater")

t.test(total_super, total_ut, paired=TRUE, var.equal=FALSE)
t.test(total_super, total_ut, paired=TRUE, var.equal=FALSE, alternative="less")
t.test(total_super, total_ut, paired=TRUE, var.equal=FALSE, alternative="greater")

#Calculando ganhos gerais

total_gut <- c(gut_100_1_20, gut_100_1_m20, gut_100_1_0, gut_100_5_20, gut_100_5_0, gut_100_5_m20)
paste("Ganho geral UT 100 : ", mean(total_gut), "[ ", 1.96*sd(total_gut)/sqrt(length(total_gut))+mean(total_gut), " : ", mean(total_gut) - 1.96*sd(total_gut)/sqrt(length(total_gut)), " ] ")

total_gop <- c(gop_100_1_20, gop_100_1_0, gop_100_1_m20, gop_100_5_20, gop_100_5_0, gop_100_5_m20)
paste("Ganho geral RF 100 : ", mean(total_gop), "[ ", 1.96*sd(total_gop)/sqrt(length(total_gop))+mean(total_gop), " : ", mean(total_gop) - 1.96*sd(total_gop)/sqrt(length(total_gop)), " ] ")

total_giosup <- c(giosup_100_1_20, giosup_100_1_0, giosup_100_1_m20, giosup_100_5_20, giosup_100_5_0, giosup_100_5_m20)
paste("Ganho geral iosup 100 : ", mean(total_giosup), "[ ", 1.96*sd(total_giosup)/sqrt(length(total_giosup))+mean(total_giosup), " : ", mean(total_giosup) - 1.96*sd(total_giosup)/sqrt(length(total_giosup)), " ] ")

total_gsuper <- c(gsuper_100_1_20, gsuper_100_1_0, gsuper_100_1_m20, gsuper_100_5_20, gsuper_100_5_0, gsuper_100_5_m20)
paste("Ganho geral super 100 : ", mean(total_gsuper), "[ ", 1.96*sd(total_gsuper)/sqrt(length(total_gsuper))+mean(total_gsuper), " : ", mean(total_gsuper) - 1.96*sd(total_gsuper)/sqrt(length(total_gsuper)), " ] ")

wilcox.test(total_gut, total_gop, paired=TRUE, var.equal=FALSE)
wilcox.test(total_gut, total_gop, paired=TRUE, var.equal=FALSE, alternative="less")
wilcox.test(total_gut, total_gop, paired=TRUE, var.equal=FALSE, alternative="greater")

wilcox.test(total_giosup, total_gop, paired=TRUE, var.equal=FALSE)
wilcox.test(total_giosup, total_gop, paired=TRUE, var.equal=FALSE, alternative="less")
wilcox.test(total_giosup, total_gop, paired=TRUE, var.equal=FALSE, alternative="greater")

wilcox.test(total_giosup, total_gut, paired=TRUE, var.equal=FALSE)
wilcox.test(total_giosup, total_gut, paired=TRUE, var.equal=FALSE, alternative="less")
wilcox.test(total_giosup, total_gut, paired=TRUE, var.equal=FALSE, alternative="greater")

wilcox.test(total_gsuper, total_gop, paired=TRUE, var.equal=FALSE)
wilcox.test(total_gsuper, total_gop, paired=TRUE, var.equal=FALSE, alternative="less")
wilcox.test(total_gsuper, total_gop, paired=TRUE, var.equal=FALSE, alternative="greater")
